package com.ruoyi.utils;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.FrameWindow;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.List;

public class CrawlPage {

    private static String url="https://pvp.qq.com";

    public static void main(String[] args) throws Exception {
        //Htmlunit模拟的浏览器，设置css,js等支持及其它的一些简单设置
        WebClient browser = new WebClient();
        browser.getOptions().setCssEnabled(false);
        browser.getOptions().setJavaScriptEnabled(true);
        browser.getOptions().setThrowExceptionOnScriptError(false);
        browser.setAjaxController(new NicelyResynchronizingAjaxController());
        //设置等待js的加载时间
        browser.waitForBackgroundJavaScript(3000);
        //获取页面
        HtmlPage htmlPage = browser.getPage(url);

        HtmlElement  kplElement=(HtmlElement)htmlPage.getByXPath("/html/body/div[1]/div/div[2]/ul/li[3]/a[1]").get(0);
//        /match/kpl/KPL2022S1/index.shtml
        String kplSrc = kplElement.getAttribute("href");
        HtmlPage byXPath = ((DomElement)htmlPage.getByXPath("/html/body/div[1]/div/div[2]/ul/li[3]/a[1]").get(0)).click();

        HtmlElement  element=(HtmlElement)byXPath.getByXPath("/html/body/div[2]/iframe[1]").get(0);
//        schedule.html
        String src = element.getAttribute("src");

        String gameUrl=url+kplSrc.substring(0,kplSrc.lastIndexOf("/")+1)+src;

        HtmlPage gameHtmlPage = browser.getPage(gameUrl);
        FrameWindow src1 = htmlPage.getFrameByName("src");
        String s = gameHtmlPage.asXml();

//        String  span =( (HtmlElement )gameHtmlPage.getByXPath("/html/body/div[1]/div/div[1]/div[2]/div[2]/ul/li[13]/div[1]/p[2]/span").get(0)).getTextContent();

        //使用xml的方式解析获取到jsoup的document对象
        Document document = Jsoup.parse(gameHtmlPage.asXml());

//        Elements idTag = document.select("#matchBox");
        System.out.println(src);
    }

    public Document getDocumentByHtmunit() throws IOException {
        //Htmlunit模拟的浏览器，设置css,js等支持及其它的一些简单设置
        WebClient browser = new WebClient();
        browser.getOptions().setCssEnabled(false);
        browser.getOptions().setJavaScriptEnabled(true);
        browser.getOptions().setThrowExceptionOnScriptError(false);
        //获取页面
        HtmlPage htmlPage = browser.getPage(url);
        //设置等待js的加载时间
        browser.waitForBackgroundJavaScript(3000);
        HtmlElement  kplElement=(HtmlElement)htmlPage.getByXPath("/html/body/div[1]/div/div[2]/ul/li[3]/a[1]").get(0);
//        /match/kpl/KPL2022S1/index.shtml
        String kplSrc = kplElement.getAttribute("href");
        HtmlPage byXPath = ((DomElement)htmlPage.getByXPath("/html/body/div[1]/div/div[2]/ul/li[3]/a[1]").get(0)).click();
        HtmlElement  element=(HtmlElement)byXPath.getByXPath("/html/body/div[2]/iframe[1]").get(0);
//        schedule.html
        String src = element.getAttribute("src");
        String gameUrl=url+kplSrc.substring(0,kplSrc.lastIndexOf("/")+1)+src;
        HtmlPage GameHtmlPage = browser.getPage(gameUrl);
        //使用xml的方式解析获取到jsoup的document对象
        Document document = Jsoup.parse(GameHtmlPage.asXml());
        return document;
    }

    public  HtmlPage getHtmlPage(String url){
        //调用此方法时加载WebClient
        WebClient webClient = new WebClient();
        webClient.getOptions().setCssEnabled(false);
        //配置webClient
        webClient.getOptions().setCssEnabled(false); //设置CSS是否生效
        webClient.getOptions().setJavaScriptEnabled(true);   //设置JS是否生效
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());  //设置AJAX请求
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);  //设置是否抛出异常码
        webClient.getOptions().setThrowExceptionOnScriptError(false);    //设置是否抛出脚本错误
        webClient.waitForBackgroundJavaScript(3*1000);   //设置等待JS毫秒数
        webClient.getCookieManager().setCookiesEnabled(true);    //设置是否支持Cookie
        HtmlPage page = null;
        try {
            page = webClient.getPage(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return page;
    }

}